<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI简历生成器</title>
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4895ef;
            --dark: #1b263b;
            --light: #f8f9fa;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f0f4f8;
            color: #333;
            margin: 0;
            padding: 0;
            line-height: 1.6;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        header {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
            padding: 20px 0;
            text-align: center;
            border-radius: 0 0 10px 10px;
            margin-bottom: 30px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            margin: 0;
            font-size: 2.2rem;
        }
        
        .tagline {
            margin-top: 5px;
            opacity: 0.9;
        }
        
        .resume-builder {
            display: grid;
            grid-template-columns: 1fr;
            gap: 20px;
        }
        
        @media (min-width: 992px) {
            .resume-builder {
                grid-template-columns: 2fr 1fr;
            }
        }
        
        .form-section {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        }
        
        .preview-section {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
            position: sticky;
            top: 20px;
        }
        
        .section-title {
            font-size: 1.3rem;
            margin-top: 0;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #eee;
            color: var(--primary);
        }
        
        .form-group {
            margin-bottom: 15px;
        }
        
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: 600;
            color: #555;
        }
        
        input, textarea, select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-family: inherit;
            font-size: 16px;
        }
        
        textarea {
            min-height: 80px;
            resize: vertical;
        }
        
        .btn {
            background-color: var(--primary);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        
        .btn:hover {
            background-color: var(--secondary);
        }
        
        .btn-secondary {
            background-color: #6c757d;
        }
        
        .btn-secondary:hover {
            background-color: #5a6268;
        }
        
        .btn-group {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }
        
        .avatar-container {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 20px;
        }
        
        .avatar-preview {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background-color: #eee;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            position: relative;
        }
        
        .avatar-preview img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .avatar-placeholder {
            font-size: 30px;
            color: #999;
        }
        
        .avatar-upload {
            flex: 1;
        }
        
        .avatar-upload-btn {
            display: inline-block;
            padding: 8px 15px;
            background-color: #f0f0f0;
            border-radius: 5px;
            cursor: pointer;
            border: 1px solid #ddd;
            font-size: 14px;
        }
        
        .avatar-upload input {
            display: none;
        }
        
        .item-list {
            margin-bottom: 20px;
        }
        
        .item {
            background-color: #f9f9f9;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 10px;
            position: relative;
        }
        
        .remove-btn {
            position: absolute;
            top: 10px;
            right: 10px;
            background: none;
            border: none;
            color: #dc3545;
            cursor: pointer;
            font-size: 18px;
        }
        
        .add-btn {
            background: none;
            border: none;
            color: var(--primary);
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 5px;
            font-weight: 600;
        }
        
        .add-btn i {
            font-size: 18px;
        }
        
        .resume-preview {
            font-size: 14px;
        }
        
        .resume-header {
            text-align: center;
            margin-bottom: 20px;
        }
        
        .resume-name {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .resume-title {
            color: var(--primary);
            font-weight: 600;
            margin-bottom: 10px;
        }
        
        .resume-contact {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 15px;
            margin-bottom: 15px;
            color: #555;
        }
        
        .resume-section {
            margin-bottom: 20px;
        }
        
        .resume-section-title {
            font-weight: bold;
            border-bottom: 2px solid #eee;
            padding-bottom: 5px;
            margin-bottom: 10px;
            color: var(--primary);
        }
        
        .resume-item {
            margin-bottom: 15px;
        }
        
        .resume-item-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
        }
        
        .resume-item-title {
            font-weight: 600;
        }
        
        .resume-item-date {
            color: #666;
        }
        
        .resume-item-desc {
            color: #444;
        }
        
        .skill-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 5px;
        }
        
        .skill-name {
            width: 30%;
        }
        
        .skill-bar {
            width: 65%;
            height: 10px;
            background-color: #eee;
            border-radius: 5px;
            overflow: hidden;
        }
        
        .skill-level {
            height: 100%;
            background-color: var(--primary);
            border-radius: 5px;
        }
        
        .tab-container {
            margin-bottom: 20px;
        }
        
        .tabs {
            display: flex;
            border-bottom: 1px solid #ddd;
        }
        
        .tab {
            padding: 10px 20px;
            cursor: pointer;
            border-bottom: 3px solid transparent;
        }
        
        .tab.active {
            border-bottom-color: var(--primary);
            font-weight: 600;
            color: var(--primary);
        }
        
        .tab-content {
            display: none;
            padding: 15px 0;
        }
        
        .tab-content.active {
            display: block;
        }
        
        .ai-suggestions {
            background-color: #f8f9fa;
            border-radius: 5px;
            padding: 15px;
            margin-top: 20px;
            border-left: 4px solid var(--primary);
        }
        
        .ai-suggestions h4 {
            margin-top: 0;
            color: var(--primary);
        }
        
        .suggestion-list {
            margin-top: 10px;
        }
        
        .suggestion-item {
            margin-bottom: 8px;
            display: flex;
            align-items: flex-start;
            gap: 8px;
        }
        
        .suggestion-item:before {
            content: "•";
            color: var(--primary);
        }
        
        .apply-suggestion {
            margin-top: 10px;
            background-color: var(--primary);
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 3px;
            cursor: pointer;
            font-size: 14px;
        }
        
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
            margin-right: 10px;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        .history-item {
            background-color: white;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 10px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .history-item-info {
            flex: 1;
        }
        
        .history-item-title {
            font-weight: 600;
            margin-bottom: 5px;
        }
        
        .history-item-date {
            color: #666;
            font-size: 14px;
        }
        
        .history-item-actions {
            display: flex;
            gap: 10px;
        }
        
        .history-btn {
            padding: 5px 10px;
            border-radius: 3px;
            font-size: 14px;
            cursor: pointer;
        }
        
        .view-btn {
            background-color: var(--primary);
            color: white;
            border: none;
        }
        
        .download-btn {
            background-color: #28a745;
            color: white;
            border: none;
        }
        
        .delete-btn {
            background-color: #dc3545;
            color: white;
            border: none;
        }
    </style>
</head>
<body>
    <header>
        <div class="container">
            <h1>AI简历生成器</h1>
            <p class="tagline">使用AI技术创建完美的简历</p>
        </div>
    </header>
    
    <div class="container">
        <div class="tab-container">
            <div class="tabs">
                <div class="tab active" data-tab="create">创建简历</div>
                <div class="tab" data-tab="history">历史记录</div>
            </div>
        </div>
        
        <div class="tab-content active" id="create-tab">
            <div class="resume-builder">
                <div class="form-section">
                    <h2 class="section-title">基本信息</h2>
                    
                    <div class="avatar-container">
                        <div class="avatar-preview" id="avatar-preview">
                            <div class="avatar-placeholder">👤</div>
                        </div>
                        <div class="avatar-upload">
                            <label class="avatar-upload-btn" for="avatar-input">选择头像</label>
                            <input type="file" id="avatar-input" accept="image/*">
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" id="name" placeholder="请输入您的姓名">
                    </div>
                    
                    <div class="form-group">
                        <label for="job-title">求职意向</label>
                        <input type="text" id="job-title" placeholder="例如: 前端开发工程师">
                    </div>
                    
                    <div class="form-group">
                        <label for="phone">联系电话</label>
                        <input type="tel" id="phone" placeholder="请输入您的电话号码">
                    </div>
                    
                    <div class="form-group">
                        <label for="email">电子邮箱</label>
                        <input type="email" id="email" placeholder="请输入您的邮箱地址">
                    </div>
                    
                    <div class="form-group">
                        <label for="address">居住地址</label>
                        <input type="text" id="address" placeholder="请输入您的居住地址">
                    </div>
                    
                    <div class="form-group">
                        <label for="summary">个人简介</label>
                        <textarea id="summary" placeholder="简要描述您的专业背景和职业目标"></textarea>
                    </div>
                    
                    <h2 class="section-title">教育背景</h2>
                    <div class="item-list" id="education-list">
                        <!-- 教育背景项目将在这里动态添加 -->
                    </div>
                    <button class="add-btn" id="add-education">
                        <span>+</span> 添加教育背景
                    </button>
                    
                    <h2 class="section-title">工作经历</h2>
                    <div class="item-list" id="experience-list">
                        <!-- 工作经历项目将在这里动态添加 -->
                    </div>
                    <button class="add-btn" id="add-experience">
                        <span>+</span> 添加工作经历
                    </button>
                    
                    <h2 class="section-title">技能专长</h2>
                    <div class="item-list" id="skills-list">
                        <!-- 技能项目将在这里动态添加 -->
                    </div>
                    <button class="add-btn" id="add-skill">
                        <span>+</span> 添加技能
                    </button>
                    
                    <h2 class="section-title">项目经验</h2>
                    <div class="item-list" id="projects-list">
                        <!-- 项目经验将在这里动态添加 -->
                    </div>
                    <button class="add-btn" id="add-project">
                        <span>+</span> 添加项目
                    </button>
                    
                    <div class="ai-suggestions">
                        <h4>AI简历优化建议</h4>
                        <p>让AI帮助您完善简历内容</p>
                        <button class="btn" id="get-ai-suggestions">获取AI建议</button>
                        <div id="suggestions-container" style="display: none;">
                            <div class="suggestion-list" id="suggestion-list"></div>
                            <button class="apply-suggestion" id="apply-suggestions">应用建议</button>
                        </div>
                    </div>
                    
                    <div class="btn-group">
                        <button class="btn" id="save-resume">保存简历</button>
                        <button class="btn btn-secondary" id="reset-form">重置表单</button>
                    </div>
                </div>
                
                <div class="preview-section">
                    <h2 class="section-title">简历预览</h2>
                    <div class="resume-preview" id="resume-preview">
                        <div class="resume-header">
                            <div class="avatar-preview" id="preview-avatar">
                                <div class="avatar-placeholder">👤</div>
                            </div>
                            <h2 class="resume-name" id="preview-name">您的姓名</h2>
                            <div class="resume-title" id="preview-title">求职意向</div>
                            <div class="resume-contact" id="preview-contact">
                                <span>电话: </span>
                                <span>邮箱: </span>
                                <span>地址: </span>
                            </div>
                        </div>
                        
                        <div class="resume-section">
                            <h3 class="resume-section-title">个人简介</h3>
                            <div id="preview-summary">暂无个人简介</div>
                        </div>
                        
                        <div class="resume-section">
                            <h3 class="resume-section-title">教育背景</h3>
                            <div id="preview-education">暂无教育背景</div>
                        </div>
                        
                        <div class="resume-section">
                            <h3 class="resume-section-title">工作经历</h3>
                            <div id="preview-experience">暂无工作经历</div>
                        </div>
                        
                        <div class="resume-section">
                            <h3 class="resume-section-title">技能专长</h3>
                            <div id="preview-skills">暂无技能信息</div>
                        </div>
                        
                        <div class="resume-section">
                            <h3 class="resume-section-title">项目经验</h3>
                            <div id="preview-projects">暂无项目经验</div>
                        </div>
                    </div>
                    
                    <div class="btn-group" style="margin-top: 20px;">
                        <button class="btn" id="download-pdf">下载PDF</button>
                        <button class="btn btn-secondary" id="print-resume">打印简历</button>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="tab-content" id="history-tab">
            <h2 class="section-title">已保存的简历</h2>
            <div id="resume-history">
                <!-- 历史简历将在这里动态添加 -->
                <p id="no-resumes-message">暂无保存的简历</p>
            </div>
        </div>
    </div>
    
    <script>
        // 全局变量
        let resumes = JSON.parse(localStorage.getItem('resumes')) || [];
        let avatarDataUrl = '';
        
        // DOM元素
        const avatarInput = document.getElementById('avatar-input');
        const avatarPreview = document.getElementById('avatar-preview');
        const previewAvatar = document.getElementById('preview-avatar');
        const tabs = document.querySelectorAll('.tab');
        const tabContents = document.querySelectorAll('.tab-content');
        
        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            loadResumeHistory();
            setupEventListeners();
            updatePreview();
        });
        
        // 设置事件监听器
        function setupEventListeners() {
            // 标签切换
            tabs.forEach(tab => {
                tab.addEventListener('click', function() {
                    tabs.forEach(t => t.classList.remove('active'));
                    tabContents.forEach(c => c.classList.remove('active'));
                    
                    this.classList.add('active');
                    document.getElementById(`${this.dataset.tab}-tab`).classList.add('active');
                });
            });
            
            // 头像上传
            avatarInput.addEventListener('change', function(e) {
                const file = e.target.files[0];
                if (file) {
                    const reader = new FileReader();
                    reader.onload = function(event) {
                        avatarDataUrl = event.target.result;
                        avatarPreview.innerHTML = `<img src="${avatarDataUrl}" alt="头像">`;
                        previewAvatar.innerHTML = `<img src="${avatarDataUrl}" alt="头像">`;
                        updatePreview();
                    };
                    reader.readAsDataURL(file);
                }
            });
            
            // 基本信息输入
            document.querySelectorAll('#name, #job-title, #phone, #email, #address, #summary').forEach(input => {
                input.addEventListener('input', updatePreview);
            });
            
            // 添加教育背景
            document.getElementById('add-education').addEventListener('click', function() {
                addEducationItem();
            });
            
            // 添加工作经历
            document.getElementById('add-experience').addEventListener('click', function() {
                addExperienceItem();
            });
            
            // 添加技能
            document.getElementById('add-skill').addEventListener('click', function() {
                addSkillItem();
            });
            
            // 添加项目
            document.getElementById('add-project').addEventListener('click', function() {
                addProjectItem();
            });
            
            // 获取AI建议
            document.getElementById('get-ai-suggestions').addEventListener('click', getAISuggestions);
            
            // 应用建议
            document.getElementById('apply-suggestions').addEventListener('click', applyAISuggestions);
            
            // 保存简历
            document.getElementById('save-resume').addEventListener('click', saveResume);
            
            // 重置表单
            document.getElementById('reset-form').addEventListener('click', resetForm);
            
            // 下载PDF
            document.getElementById('download-pdf').addEventListener('click', downloadPDF);
            
            // 打印简历
            document.getElementById('print-resume').addEventListener('click', printResume);
        }
        
        // 添加教育背景项目
        function addEducationItem(eduData = {}) {
            const educationList = document.getElementById('education-list');
            const itemId = Date.now();
            
            const item = document.createElement('div');
            item.className = 'item';
            item.dataset.id = itemId;
            item.innerHTML = `
                <button class="remove-btn" data-id="${itemId}">×</button>
                <div class="form-group">
                    <label for="edu-school-${itemId}">学校名称</label>
                    <input type="text" id="edu-school-${itemId}" value="${eduData.school || ''}" placeholder="例如: 清华大学">
                </div>
                <div class="form-group">
                    <label for="edu-major-${itemId}">专业</label>
                    <input type="text" id="edu-major-${itemId}" value="${eduData.major || ''}" placeholder="例如: 计算机科学与技术">
                </div>
                <div class="form-group">
                    <label for="edu-degree-${itemId}">学历</label>
                    <select id="edu-degree-${itemId}">
                        <option value="">请选择学历</option>
                        <option value="高中" ${eduData.degree === '高中' ? 'selected' : ''}>高中</option>
                        <option value="大专" ${eduData.degree === '大专' ? 'selected' : ''}>大专</option>
                        <option value="本科" ${eduData.degree === '本科' ? 'selected' : ''}>本科</option>
                        <option value="硕士" ${eduData.degree === '硕士' ? 'selected' : ''}>硕士</option>
                        <option value="博士" ${eduData.degree === '博士' ? 'selected' : ''}>博士</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="edu-start-${itemId}">开始时间</label>
                    <input type="month" id="edu-start-${itemId}" value="${eduData.startDate || ''}">
                </div>
                <div class="form-group">
                    <label for="edu-end-${itemId}">结束时间</label>
                    <input type="month" id="edu-end-${itemId}" value="${eduData.endDate || ''}">
                </div>
                <div class="form-group">
                    <label for="edu-desc-${itemId}">描述</label>
                    <textarea id="edu-desc-${itemId}" placeholder="例如: 主修课程、荣誉奖项等">${eduData.description || ''}</textarea>
                </div>
            `;
            
            educationList.appendChild(item);
            
            // 添加删除事件
            item.querySelector('.remove-btn').addEventListener('click', function() {
                educationList.removeChild(item);
                updatePreview();
            });
            
            // 添加输入事件
            item.querySelectorAll('input, select, textarea').forEach(input => {
                input.addEventListener('input', updatePreview);
            });
            
            updatePreview();
        }
        
        // 添加工作经历项目
        function addExperienceItem(expData = {}) {
            const experienceList = document.getElementById('experience-list');
            const itemId = Date.now();
            
            const item = document.createElement('div');
            item.className = 'item';
            item.dataset.id = itemId;
            item.innerHTML = `
                <button class="remove-btn" data-id="${itemId}">×</button>
                <div class="form-group">
                    <label for="exp-company-${itemId}">公司名称</label>
                    <input type="text" id="exp-company-${itemId}" value="${expData.company || ''}" placeholder="例如: 腾讯科技">
                </div>
                <div class="form-group">
                    <label for="exp-position-${itemId}">职位</label>
                    <input type="text" id="exp-position-${itemId}" value="${expData.position || ''}" placeholder="例如: 前端开发工程师">
                </div>
                <div class="form-group">
                    <label for="exp-start-${itemId}">开始时间</label>
                    <input type="month" id="exp-start-${itemId}" value="${expData.startDate || ''}">
                </div>
                <div class="form-group">
                    <label for="exp-end-${itemId}">结束时间</label>
                    <input type="month" id="exp-end-${itemId}" value="${expData.endDate || ''}">
                </div>
                <div class="form-group">
                    <label for="exp-desc-${itemId}">工作描述</label>
                    <textarea id="exp-desc-${itemId}" placeholder="描述您的工作职责和成就">${expData.description || ''}</textarea>
                </div>
            `;
            
            experienceList.appendChild(item);
            
            // 添加删除事件
            item.querySelector('.remove-btn').addEventListener('click', function() {
                experienceList.removeChild(item);
                updatePreview();
            });
            
            // 添加输入事件
            item.querySelectorAll('input, textarea').forEach(input => {
                input.addEventListener('input', updatePreview);
            });
            
            updatePreview();
        }
        
        // 添加技能项目
        function addSkillItem(skillData = {}) {
            const skillsList = document.getElementById('skills-list');
            const itemId = Date.now();
            
            const item = document.createElement('div');
            item.className = 'item';
            item.dataset.id = itemId;
            item.innerHTML = `
                <button class="remove-btn" data-id="${itemId}">×</button>
                <div class="form-group">
                    <label for="skill-name-${itemId}">技能名称</label>
                    <input type="text" id="skill-name-${itemId}" value="${skillData.name || ''}" placeholder="例如: JavaScript">
                </div>
                <div class="form-group">
                    <label for="skill-level-${itemId}">熟练程度</label>
                    <select id="skill-level-${itemId}">
                        <option value="20" ${skillData.level === '20' ? 'selected' : ''}>入门</option>
                        <option value="40" ${skillData.level === '40' ? 'selected' : ''}>一般</option>
                        <option value="60" ${skillData.level === '60' ? 'selected' : ''}>熟练</option>
                        <option value="80" ${skillData.level === '80' ? 'selected' : ''}>精通</option>
                        <option value="100" ${skillData.level === '100' ? 'selected' : ''}>专家</option>
                    </select>
                </div>
            `;
            
            skillsList.appendChild(item);
            
            // 添加删除事件
            item.querySelector('.remove-btn').addEventListener('click', function() {
                skillsList.removeChild(item);
                updatePreview();
            });
            
            // 添加输入事件
            item.querySelectorAll('input, select').forEach(input => {
                input.addEventListener('input', updatePreview);
            });
            
            updatePreview();
        }
        
        // 添加项目经验
        function addProjectItem(projectData = {}) {
            const projectsList = document.getElementById('projects-list');
            const itemId = Date.now();
            
            const item = document.createElement('div');
            item.className = 'item';
            item.dataset.id = itemId;
            item.innerHTML = `
                <button class="remove-btn" data-id="${itemId}">×</button>
                <div class="form-group">
                    <label for="project-name-${itemId}">项目名称</label>
                    <input type="text" id="project-name-${itemId}" value="${projectData.name || ''}" placeholder="例如: 电商网站开发">
                </div>
                <div class="form-group">
                    <label for="project-role-${itemId}">担任角色</label>
                    <input type="text" id="project-role-${itemId}" value="${projectData.role || ''}" placeholder="例如: 前端开发负责人">
                </div>
                <div class="form-group">
                    <label for="project-start-${itemId}">开始时间</label>
                    <input type="month" id="project-start-${itemId}" value="${projectData.startDate || ''}">
                </div>
                <div class="form-group">
                    <label for="project-end-${itemId}">结束时间</label>
                    <input type="month" id="project-end-${itemId}" value="${projectData.endDate || ''}">
                </div>
                <div class="form-group">
                    <label for="project-desc-${itemId}">项目描述</label>
                    <textarea id="project-desc-${itemId}" placeholder="描述项目内容和您的贡献">${projectData.description || ''}</textarea>
                </div>
            `;
            
            projectsList.appendChild(item);
            
            // 添加删除事件
            item.querySelector('.remove-btn').addEventListener('click', function() {
                projectsList.removeChild(item);
                updatePreview();
            });
            
            // 添加输入事件
            item.querySelectorAll('input, textarea').forEach(input => {
                input.addEventListener('input', updatePreview);
            });
            
            updatePreview();
        }
        
        // 更新简历预览
        function updatePreview() {
            // 基本信息
            document.getElementById('preview-name').textContent = document.getElementById('name').value || '您的姓名';
            document.getElementById('preview-title').textContent = document.getElementById('job-title').value || '求职意向';
            
            const contactInfo = [
                document.getElementById('phone').value ? `电话: ${document.getElementById('phone').value}` : '',
                document.getElementById('email').value ? `邮箱: ${document.getElementById('email').value}` : '',
                document.getElementById('address').value ? `地址: ${document.getElementById('address').value}` : ''
            ].filter(Boolean).join(' | ');
            
            document.getElementById('preview-contact').innerHTML = contactInfo || '暂无联系方式';
            
            // 个人简介
            const summary = document.getElementById('summary').value;
            document.getElementById('preview-summary').innerHTML = summary || '暂无个人简介';
            
            // 教育背景
            const educationItems = document.querySelectorAll('#education-list .item');
            let educationHTML = '';
            
            if (educationItems.length > 0) {
                educationItems.forEach(item => {
                    const id = item.dataset.id;
                    const school = document.getElementById(`edu-school-${id}`).value;
                    const major = document.getElementById(`edu-major-${id}`).value;
                    const degree = document.getElementById(`edu-degree-${id}`).value;
                    const startDate = document.getElementById(`edu-start-${id}`).value;
                    const endDate = document.getElementById(`edu-end-${id}`).value;
                    const description = document.getElementById(`edu-desc-${id}`).value;
                    
                    if (school || major || degree) {
                        educationHTML += `
                            <div class="resume-item">
                                <div class="resume-item-header">
                                    <span class="resume-item-title">${school || '未知学校'}</span>
                                    <span class="resume-item-date">${startDate || ''} - ${endDate || '至今'}</span>
                                </div>
                                <div class="resume-item-desc">
                                    ${degree ? `${degree} | ` : ''}${major || '未知专业'}
                                    ${description ? `<br>${description}` : ''}
                                </div>
                            </div>
                        `;
                    }
                });
            } else {
                educationHTML = '暂无教育背景';
            }
            
            document.getElementById('preview-education').innerHTML = educationHTML;
            
            // 工作经历
            const experienceItems = document.querySelectorAll('#experience-list .item');
            let experienceHTML = '';
            
            if (experienceItems.length > 0) {
                experienceItems.forEach(item => {
                    const id = item.dataset.id;
                    const company = document.getElementById(`exp-company-${id}`).value;
                    const position = document.getElementById(`exp-position-${id}`).value;
                    const startDate = document.getElementById(`exp-start-${id}`).value;
                    const endDate = document.getElementById(`exp-end-${id}`).value;
                    const description = document.getElementById(`exp-desc-${id}`).value;
                    
                    if (company || position) {
                        experienceHTML += `
                            <div class="resume-item">
                                <div class="resume-item-header">
                                    <span class="resume-item-title">${company || '未知公司'} | ${position || '未知职位'}</span>
                                    <span class="resume-item-date">${startDate || ''} - ${endDate || '至今'}</span>
                                </div>
                                <div class="resume-item-desc">
                                    ${description || '暂无详细描述'}
                                </div>
                            </div>
                        `;
                    }
                });
            } else {
                experienceHTML = '暂无工作经历';
            }
            
            document.getElementById('preview-experience').innerHTML = experienceHTML;
            
            // 技能专长
            const skillItems = document.querySelectorAll('#skills-list .item');
            let skillsHTML = '';
            
            if (skillItems.length > 0) {
                skillItems.forEach(item => {
                    const id = item.dataset.id;
                    const name = document.getElementById(`skill-name-${id}`).value;
                    const level = document.getElementById(`skill-level-${id}`).value;
                    
                    if (name) {
                        skillsHTML += `
                            <div class="skill-item">
                                <span class="skill-name">${name}</span>
                                <div class="skill-bar">
                                    <div class="skill-level" style="width: ${level}%"></div>
                                </div>
                            </div>
                        `;
                    }
                });
            } else {
                skillsHTML = '暂无技能信息';
            }
            
            document.getElementById('preview-skills').innerHTML = skillsHTML;
            
            // 项目经验
            const projectItems = document.querySelectorAll('#projects-list .item');
            let projectsHTML = '';
            
            if (projectItems.length > 0) {
                projectItems.forEach(item => {
                    const id = item.dataset.id;
                    const name = document.getElementById(`project-name-${id}`).value;
                    const role = document.getElementById(`project-role-${id}`).value;
                    const startDate = document.getElementById(`project-start-${id}`).value;
                    const endDate = document.getElementById(`project-end-${id}`).value;
                    const description = document.getElementById(`project-desc-${id}`).value;
                    
                    if (name || role) {
                        projectsHTML += `
                            <div class="resume-item">
                                <div class="resume-item-header">
                                    <span class="resume-item-title">${name || '未知项目'} | ${role || '未知角色'}</span>
                                    <span class="resume-item-date">${startDate || ''} - ${endDate || '至今'}</span>
                                </div>
                                <div class="resume-item-desc">
                                    ${description || '暂无详细描述'}
                                </div>
                            </div>
                        `;
                    }
                });
            } else {
                projectsHTML = '暂无项目经验';
            }
            
            document.getElementById('preview-projects').innerHTML = projectsHTML;
        }
        
        // 获取AI建议
        async function getAISuggestions() {
            const btn = document.getElementById('get-ai-suggestions');
            const originalText = btn.textContent;
            
            btn.disabled = true;
            btn.innerHTML = '<span class="loading"></span>正在获取AI建议...';
            
            try {
                // 收集简历数据
                const resumeData = collectResumeData();
                
                // 调用DeepSeek API
                const response = await fetch('https://api.deepseek.com/v1/chat/completions', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer sk-c6a7fb0a88f04c45a858716ce9aec15c`
                    },
                    body: JSON.stringify({
                        model: "deepseek-chat",
                        messages: [
                            {
                                role: "system",
                                content: "你是一个专业的简历优化专家，请根据用户提供的简历内容，给出具体的优化建议。建议应针对简历的各个部分，包括但不限于：格式优化、内容补充、表达方式改进等。请用中文回答，并给出具体的修改建议。"
                            },
                            {
                                role: "user",
                                content: `请为以下简历提供优化建议：\n\n${JSON.stringify(resumeData, null, 2)}`
                            }
                        ],
                        temperature: 0.7
                    })
                });
                
                const data = await response.json();
                const suggestions = data.choices[0].message.content;
                
                // 显示建议
                const suggestionsContainer = document.getElementById('suggestions-container');
                const suggestionList = document.getElementById('suggestion-list');
                
                // 解析AI返回的建议
                const suggestionItems = suggestions.split('\n').filter(item => item.trim());
                
                suggestionList.innerHTML = suggestionItems.map(item => {
                    return `<div class="suggestion-item">${item}</div>`;
                }).join('');
                
                suggestionsContainer.style.display = 'block';
                
            } catch (error) {
                console.error('获取AI建议失败:', error);
                alert('获取AI建议失败，请稍后再试');
            } finally {
                btn.disabled = false;
                btn.textContent = originalText;
            }
        }
        
        // 应用AI建议
        function applyAISuggestions() {
            // 这里可以添加逻辑来实际应用AI建议
            alert('AI建议已应用到您的简历中！');
            document.getElementById('suggestions-container').style.display = 'none';
        }
        
        // 收集简历数据
        function collectResumeData() {
            const data = {
                personalInfo: {
                    name: document.getElementById('name').value,
                    jobTitle: document.getElementById('job-title').value,
                    phone: document.getElementById('phone').value,
                    email: document.getElementById('email').value,
                    address: document.getElementById('address').value,
                    summary: document.getElementById('summary').value,
                    avatar: avatarDataUrl
                },
                education: [],
                experience: [],
                skills: [],
                projects: []
            };
            
            // 收集教育背景
            document.querySelectorAll('#education-list .item').forEach(item => {
                const id = item.dataset.id;
                data.education.push({
                    school: document.getElementById(`edu-school-${id}`).value,
                    major: document.getElementById(`edu-major-${id}`).value,
                    degree: document.getElementById(`edu-degree-${id}`).value,
                    startDate: document.getElementById(`edu-start-${id}`).value,
                    endDate: document.getElementById(`edu-end-${id}`).value,
                    description: document.getElementById(`edu-desc-${id}`).value
                });
            });
            
            // 收集工作经历
            document.querySelectorAll('#experience-list .item').forEach(item => {
                const id = item.dataset.id;
                data.experience.push({
                    company: document.getElementById(`exp-company-${id}`).value,
                    position: document.getElementById(`exp-position-${id}`).value,
                    startDate: document.getElementById(`exp-start-${id}`).value,
                    endDate: document.getElementById(`exp-end-${id}`).value,
                    description: document.getElementById(`exp-desc-${id}`).value
                });
            });
            
            // 收集技能
            document.querySelectorAll('#skills-list .item').forEach(item => {
                const id = item.dataset.id;
                data.skills.push({
                    name: document.getElementById(`skill-name-${id}`).value,
                    level: document.getElementById(`skill-level-${id}`).value
                });
            });
            
            // 收集项目经验
            document.querySelectorAll('#projects-list .item').forEach(item => {
                const id = item.dataset.id;
                data.projects.push({
                    name: document.getElementById(`project-name-${id}`).value,
                    role: document.getElementById(`project-role-${id}`).value,
                    startDate: document.getElementById(`project-start-${id}`).value,
                    endDate: document.getElementById(`project-end-${id}`).value,
                    description: document.getElementById(`project-desc-${id}`).value
                });
            });
            
            return data;
        }
        
        // 保存简历
        function saveResume() {
            const resumeData = collectResumeData();
            const resumeId = Date.now();
            const resumeName = resumeData.personalInfo.name ? `${resumeData.personalInfo.name}的简历` : `未命名简历-${new Date().toLocaleDateString()}`;
            
            const resume = {
                id: resumeId,
                name: resumeName,
                date: new Date().toLocaleDateString(),
                data: resumeData
            };
            
            resumes.push(resume);
            localStorage.setItem('resumes', JSON.stringify(resumes));
            
            alert('简历保存成功！');
            loadResumeHistory();
        }
        
        // 加载历史简历
        function loadResumeHistory() {
            const historyContainer = document.getElementById('resume-history');
            const noResumesMessage = document.getElementById('no-resumes-message');
            
            if (resumes.length === 0) {
                noResumesMessage.style.display = 'block';
                historyContainer.innerHTML = '';
                return;
            }
            
            noResumesMessage.style.display = 'none';
            
            historyContainer.innerHTML = resumes.map(resume => `
                <div class="history-item" data-id="${resume.id}">
                    <div class="history-item-info">
                        <div class="history-item-title">${resume.name}</div>
                        <div class="history-item-date">保存于: ${resume.date}</div>
                    </div>
                    <div class="history-item-actions">
                        <button class="history-btn view-btn" data-id="${resume.id}">查看</button>
                        <button class="history-btn download-btn" data-id="${resume.id}">下载</button>
                        <button class="history-btn delete-btn" data-id="${resume.id}">删除</button>
                    </div>
                </div>
            `).join('');
            
            // 添加历史记录按钮事件
            document.querySelectorAll('.view-btn').forEach(btn => {
                btn.addEventListener('click', function() {
                    const resumeId = parseInt(this.dataset.id);
                    loadResume(resumeId);
                    
                    // 切换到创建标签
                    tabs.forEach(t => t.classList.remove('active'));
                    tabContents.forEach(c => c.classList.remove('active'));
                    
                    document.querySelector('.tab[data-tab="create"]').classList.add('active');
                    document.getElementById('create-tab').classList.add('active');
                });
            });
            
            document.querySelectorAll('.download-btn').forEach(btn => {
                btn.addEventListener('click', function() {
                    const resumeId = parseInt(this.dataset.id);
                    const resume = resumes.find(r => r.id === resumeId);
                    if (resume) {
                        downloadResumeAsPDF(resume);
                    }
                });
            });
            
            document.querySelectorAll('.delete-btn').forEach(btn => {
                btn.addEventListener('click', function() {
                    if (confirm('确定要删除这个简历吗？')) {
                        const resumeId = parseInt(this.dataset.id);
                        resumes = resumes.filter(r => r.id !== resumeId);
                        localStorage.setItem('resumes', JSON.stringify(resumes));
                        loadResumeHistory();
                    }
                });
            });
        }
        
        // 加载特定简历
        function loadResume(resumeId) {
            const resume = resumes.find(r => r.id === resumeId);
            if (!resume) return;
            
            const data = resume.data;
            
            // 重置表单
            resetForm(false);
            
            // 设置基本信息
            document.getElementById('name').value = data.personalInfo.name || '';
            document.getElementById('job-title').value = data.personalInfo.jobTitle || '';
            document.getElementById('phone').value = data.personalInfo.phone || '';
            document.getElementById('email').value = data.personalInfo.email || '';
            document.getElementById('address').value = data.personalInfo.address || '';
            document.getElementById('summary').value = data.personalInfo.summary || '';
            
            // 设置头像
            if (data.personalInfo.avatar) {
                avatarDataUrl = data.personalInfo.avatar;
                avatarPreview.innerHTML = `<img src="${avatarDataUrl}" alt="头像">`;
                previewAvatar.innerHTML = `<img src="${avatarDataUrl}" alt="头像">`;
            }
            
            // 添加教育背景
            data.education.forEach(edu => {
                addEducationItem(edu);
            });
            
            // 添加工作经历
            data.experience.forEach(exp => {
                addExperienceItem(exp);
            });
            
            // 添加技能
            data.skills.forEach(skill => {
                addSkillItem(skill);
            });
            
            // 添加项目经验
            data.projects.forEach(project => {
                addProjectItem(project);
            });
            
            updatePreview();
        }
        
        // 重置表单
        function resetForm(confirm = true) {
            if (confirm && !confirm('确定要重置表单吗？所有未保存的更改将丢失。')) {
                return;
            }
            
            // 清除表单
            document.getElementById('name').value = '';
            document.getElementById('job-title').value = '';
            document.getElementById('phone').value = '';
            document.getElementById('email').value = '';
            document.getElementById('address').value = '';
            document.getElementById('summary').value = '';
            
            // 清除头像
            avatarDataUrl = '';
            avatarPreview.innerHTML = '<div class="avatar-placeholder">👤</div>';
            previewAvatar.innerHTML = '<div class="avatar-placeholder">👤</div>';
            
            // 清除动态添加的项目
            document.getElementById('education-list').innerHTML = '';
            document.getElementById('experience-list').innerHTML = '';
            document.getElementById('skills-list').innerHTML = '';
            document.getElementById('projects-list').innerHTML = '';
            
            // 隐藏AI建议
            document.getElementById('suggestions-container').style.display = 'none';
            
            updatePreview();
        }
        
        // 下载PDF
        function downloadPDF() {
            alert('在实际应用中，这里会使用PDF生成库将简历转换为PDF并下载');
            // 实际实现可以使用jsPDF或html2pdf.js等库
        }
        
        // 打印简历
        function printResume() {
            window.print();
        }
        
        // 下载特定简历为PDF
        function downloadResumeAsPDF(resume) {
            alert(`正在下载简历: ${resume.name}`);
            // 实际实现可以使用jsPDF或html2pdf.js等库
        }
    </script>
</body>
</html>